function diff(oldTree,newTree) {
    let patches={};
    let index=0;
    //递归树 比较后的结果放到补丁包种
    walk(oldTree,newTree,index,patches);
    return patches
}
function  diffAttr(oldAttrs,newAttrs){
    let patch={};
    //判断老的属性和新的属性的关系
    for(let key in oldAttrs){
        if(oldAttrs[key]!==newAttrs[key]){
            patch[key]=newAttrs[key];//有的可能是undefined
        }
    }
    //判断新增的
    for(let key in newAttrs){
        //老节点没有新节点的属性
       if(!oldAttrs.hasOwnProperty(key)){
           patch[key]=newAttrs[key];
       }
    }
    return patch
}
const ATTRS='ATTRS';
const TEXT="TEXT";
const REMOVE="REMOVE";
const REPLACE='REPLACE';
let Index=0;
function diffChildren(oldChildren,newChildren,index,patches) {
//    比较老的第一个和新的第一个
    oldChildren.forEach((child,idx)=>{
        walk(child,newChildren[idx],++Index,patches)
    })
}
function isString(node) {
    return Object.prototype.toString.call(node)==='[object String]'
}
function  walk(oldNode,newNode,index,patches){
   let currentPath=[];
   if(!newNode){
       currentPath.push({type:REMOVE,index})
   }
   else if(isString(oldNode)&&isString(newNode)){//判断文本是否一致
       if(oldNode!==newNode){
           currentPath.push({type:TEXT,newNode})
       }
   } else if(oldNode.type===newNode.type){
       //比较属性是否有更改
      let attrs=diffAttr(oldNode.props,newNode.props);
      if(Object.keys(attrs).length>0){
          currentPath.push({type:ATTRS,attrs})
      }
       // 如果有子节点 遍历子
       diffChildren(oldNode.children,newNode.children,index,patches);

   }else{
   //     说明节点被替换了
       currentPath.push({type:REPLACE,newNode})
   }
    if(currentPath.length){
        patches[index]=currentPath;

    }

}
export default diff